762B - USB vs PS-2 - CodeForces Solution


greedy implementation sortings two pointers *1400

Please click on ads to support us..

C++ Code:

    #include<bits/stdc++.h>
    #pragma GCC optimize ("Ofast")
    using namespace std;
     
    typedef long long int ll;
    typedef vector<ll> vi;
    typedef pair<ll,ll> pi;
     
    #define PB push_back
    #define MP make_pair
    #define F first
    #define S second
     
    const ll MOD=1000000007;
  
  

   void solve(){
     ll a,b,c;cin>>a>>b>>c;
     ll m;cin>>m;

     ll tc=0,ans=0;
     vi usb,ps;
     for(int i=0;i<m;i++){
      string s;ll x;cin>>x>>s;
      if(s=="USB"){
        usb.push_back(x);
      }
      else{
        ps.push_back(x);
      }
     }
     sort(usb.begin(),usb.end());
     sort(ps.begin(),ps.end());
     ll i=0,j=0;
     while(a>0 && usb.size()>i){
       ans+=usb[i];
       i++;
       a--;
       tc++;
     }  
     while(b>0 &&ps.size()>j){
      ans+=ps[j];
      j++;
      b--;
      tc++;
     }
     //cout<<ans<<" ";
     if(a==0 || b==0){
        vi d;
        for(int k=i;k<usb.size();k++){
          d.push_back(usb[k]);
        }   
        for(int k=j;k<ps.size();k++){
          d.push_back(ps[k]);
        }
        sort(d.begin(),d.end());
        int  v=0;
        while(c>0 && d.size()>v){
          ans+=d[v];
          tc++;
          c--;
          v++;
        }
        cout<<tc<<" "<<ans<<"\n";
     }
     else{
         cout<<tc<<" "<<ans<<"\n";
     }
    }
    int main()
    {
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
    
      ll t=1;
      
      while(t--){
        solve();
      }
      return 0;
    }


Comments

Submit
0 Comments
More Questions

622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes